<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
    </script>
</body>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <h3>队列</h3>
    <script>
        //对队列结构进行一个封装
        class Queue {
            constructor() {
                    this.item = [];
                }
                //插入数据
            enqueue(element) {
                    this.item.push(element);
                }
                //删除
            dequeue() {
                    return this.item.shift();
                }
                //返回第一个元素
            peek() {
                    if (this.isEmpty()) {
                        return undefined;
                    }
                    return this.item[0];
                }
                //判断是否为空
            isEmpty() {
                return this.item.length === 0;
            }
            size() {
                return this.item.length;
            }

            toString() {
                return this.item.toString();
            }
        }
        //击鼓传花
        //组织人员
        function playGame(player, num) {
            //应用队列
            const queue = new Queue();
            //将游戏玩家存储到队列当中
            for (let i = 0; i < player.length; i++) {
                queue.enqueue(player[i]);
            }
            console.log("参与游戏的玩家:", queue.toString());
            //循环进行游戏
            while (queue.size() > 1) {

                for (let i = 0; i < num - 1; i++) {

                    queue.enqueue(queue.dequeue());
                }

                outedPlayer = queue.dequeue();
                console.log("被淘汰的玩家:", outedPlayer);
            }

            console.log("获胜玩家:", queue.toString());
        }
        const player = ['a', 'b', 'c', 'd', 'e', 'f'];
        const num = 3;

        playGame(player, num);
    </script>
</body>

</html>

</html>